<?php
namespace app\api\controller\v1;
use x\my\Captcha;
class User extends Base
{
	//用户手机短信登陆
    public function smslogin(){
        $d = $this->post;
        $rule = [
            'username'=>'require',
            'password'=>'require',
            'img_token'=>'require',
        ];
        $msg = [
            'username.require' => '请输入用户名',
            'password.require' => '请输入密码',
        ];
        if(!Captcha::checkCaptcha($d['img_token'],$d['code']) ){
            cookie('js_name','code');
            err('验证码错误!');
        }


        $password = decode($d['password']);
        $rs = m('User','c')->login($d['username'],$password);
        if($rs){
            cookie('js_msg','登录成功!');
            return suc(['token'=>$rs]);
        }
        err(m('User','c')->getError());
    }

    //忘记密码
    public function forget(){
        $d = $this->post;
        $rule = [
            'userphone'=>'require',
            'password'=>'require',
            'code'=>'require',
            'agreement' => 'require',
        ];
        $msg = [
            'userphone.require' => '请输入手机号',
            'password.require' => '请输入新密码',
            'code.require' => '请输入验证码',
            'agreement.require' => '请同意协议',
        ];
        check($rule,$msg,$d);
        //短信验证
        if(!m('sms','xcom')->checkCode($d['userphone'], 'forget', $d['code'])){
            err(m('sms','xcom')->getError());
        }
        $password = decode($d['password']);
        $rs = m('User','c')->forget($d['userphone'],$password);
        if($rs){
            cookie('js_msg','重置成功!');
            return suc();
        }
        err(m('User','c')->getError());
    }

    //注册
    public function register(){
        $d = $this->post;
        //判断短信验证码
        $rule = [
            'username'=>'require',
            'userphone'=>'require',
            'password'=>'require',
            'code'=>'require',
            'agreement' => 'require',
        ];
        $msg = [
            'username.require' => '请输入用户名',
            'userphone.require' => '请输入手机号',
            'password.require' => '请输入密码',
            'code.require' => '请输入验证码',
            'agreement.require' => '请同意协议',
        ];
        check($rule,$msg,$d);
        //短信验证
        if(!m('sms','xcom')->checkCode($d['userphone'], 'register', $d['code'])){
            err(m('sms','xcom')->getError());
        }
        $password = decode($d['password']);
        $rs = m('User','c')->register($d['username'],$d['userphone'],$password);
        if($rs){
            cookie('js_url',url('Publics/login'));
            cookie('js_msg','注册成功!');
            return suc();
        }
        err(m('User','c')->getError());
    }

    //退出
    public function logout(){
        $token = cookie('token');
        if($token){
            $this->db->update('user',['token'=>''],['token=?',[$token]]);
        }
        cookie(null,null);
        cookie('js_msg','退出成功!');
        return suc();
    }

    //基本资料
    public function details(){
        $this->checkLogin();
        //用户银行
        $userbank = $this->db->query("select * from user_bank where user_id=?",[$this->userinfo["id"]],1);
        suc(['userinfo'=>$this->userinfo,'userbank'=>$userbank]);
    }

    //基本资料
    public function edit(){
        $this->checkLogin();
        $d = $this->post;
        $rule = [
            'username'=>'require',
            'name'=>'require',
        ];
        $msg = [
            'username.require' => '请输入用户名',
            'name.require' => '请输入真实姓名',
        ];
        check($rule,$msg,$d);
        // if(!empty($d["banknumber"])){
        //     if(strlen($d["banknumber"]) != 16 && strlen($d["banknumber"]) != 19){
        //         err('请填写正确的银行卡卡号!');
        //     }
        // }
        $d['user_id'] = $this->userinfo["id"];
        $re = m('user','c')->saveUser($d);
        if($re){
            return suc("保存成功");
        }else{
            err('保存错误!');
        }
    }

    //用户手机更改
    public function editPhone(){
        $this->checkLogin();
        $d = $this->post;
        $rule = [
            'newtel'=>'require',
            'password'=>'require',
            'code'=>'require',
        ];
        $msg = [
            'newtel.require' => '请输入手机号',
            'password.require' => '请输入登陆密码',
            'code.require' => '请输入安证码',
        ];
        check($rule,$msg,$d);
        //短信验证
        if(!m('sms','xcom')->checkCode($d['newtel'], 'newtel', $d['code'])){
            err(m('sms','xcom')->getError());
        }
        $rs = $this->db->query('select id from user where phone=?',[$d['newtel']],1);
        if($rs){
            err('该手机号已存在!');
        }
        $password = decode($d['password']);
        $data = [
            "phone" => $d['newtel'],
            "password" => com_password($password),
            "update_time" => time()
        ];
        $result = $this->db->update("user",$data,["id=?",[$this->userinfo["id"]]]);

        if($result){
            suc('保存成功');
        }else{
            err('保存错误!');
        }

    }

    //用户密码
    public function editPwd(){
        $this->checkLogin();
        $d = $this->post;
        $rule = [
            'oldpassword'=>'require|min:6',
            'newpassword'=>'require|min:6',
            'repassword'=>'require|min:6',
            'code'=>'require',
        ];
        $msg = [
            'oldpassword.require' => '请输入原密码',
            'oldpassword.minlenght' => '密码至少为六位数',
            'newpassword.require' => '请输入新密码',
            'newpassword.minlenght' => '密码至少为六位数',
            'repassword.require' => '请输入确认密码',
            'repassword.minlenght' => '密码至少为六位数',
            'code.require' => '请输入安证码',
        ];
        check($rule,$msg,$d);
        if($d["newpassword"] != $d["repassword"]){
            err('两次密码不一致');
        }
        //短信验证
        if(!m('sms','xcom')->checkCode($this->userinfo["phone"], 'remake', $d['code'])){
            err(m('sms','xcom')->getError());
        }
        $password = decode($d['newpassword']);
        $data = [
            "password" => new_com_password($password,$this->userinfo["salt"]),
            "update_time" => time()
        ];
        $re = $this->db->update("user",$data,["id=?",[$this->userinfo["id"]]]);
        if($re){
            return suc('保存成功');
        }else{
            err('保存错误!');
        }
    }
}
